import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class HttpServerResponseHandler extends ChannelInboundHandlerAdapter {

	private Object httpMessage;
	private ChannelHandlerContext ctx;

	public HttpServerResponseHandler(ChannelHandlerContext ctx, Object o) {
		this.httpMessage = o;
		this.ctx = ctx;
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		ctx.writeAndFlush(httpMessage); // 必须有flush
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) {
		System.out.println("网页请求结果=========================");
		System.out.println(msg);
		System.out.println();
		this.ctx.writeAndFlush(ctx);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
		// Close the connection when an exception is raised.
		cause.printStackTrace();
		ctx.close();
	}
}
